<template>
  <div>
    <el-steps :active="active" finish-status="success">
      <el-step title="请选择仓库"></el-step>
      <el-step title="请选择设备ip"></el-step>
    </el-steps>
    <div
      v-show="onediv"
      style="display: flex;flex-wrap: wrap;  justify-content: space-between"
    >
      <!-- 引导1 -->
      <div v-for="(item, index) in Warehouse" :key="index" style="flex: 1">
        <el-card :body-style="{ padding: '2rem' }">
          <img
            :src="require(`../assets/ck1.jpg`)"
            alt=""
            style="width: 100%; height: 100%"
          />
          <div>
            <div class="bottom clearfix">
              <el-button
                @click="next(item)"
                style="width: 100%;height: 60px;"
                type="primary"
                >{{ item.warehouseName }}</el-button
              >
            </div>
          </div>
        </el-card>
      </div>
    </div>
    <!-- 引导2 -->
    <div v-show="twodiv" style="width: 70%; height: 900px; margin: 0 auto">
      <el-link :underline="false" icon="el-icon-arrow-left" @click="comeback"
        >返回</el-link
      >
      <el-form
        :model="myfrom"
        :rules="rules"
        ref="myfrom"
        label-width="80px"
        label-position="top"
      >
        <el-form-item label="门内摄像头IP" prop="inDoorCameraIp">
          <el-input
            v-model="myfrom.inDoorCameraIp"
            placeholder="请输入门内摄像头IP"
          ></el-input>
        </el-form-item>
        <el-form-item label="门外摄像头IP" prop="outDoorCameraIp">
          <el-input
            v-model="myfrom.outDoorCameraIp"
            placeholder="请输入门内摄像头IP"
          ></el-input>
        </el-form-item>
        <el-form-item label="门禁设备IP" prop="doorIp">
          <el-input
            v-model="myfrom.doorIp"
            placeholder="请输入门禁设备IP"
          ></el-input>
        </el-form-item>
        <el-form-item label="归还时间" prop="longTimeNoReturn">
          <el-input
            v-model="myfrom.longTimeNoReturn"
            placeholder="请设置归还时间"
          ></el-input>
        </el-form-item>
      </el-form>
      <span style="float: right; margin-top: 3rem" v-show="buttonshow">
        <el-button type="primary" @click="submitform">确 定</el-button>
      </span>
    </div>
  </div>
</template>

<script>
import {
  setConfigByWarehouse,
  listAllWarehouse,
  setShowWarehouseId,
} from "@/api/index";
export default {
  data() {
    return {
      loading: null,
      currentDate: new Date(),
      rules: {
        inDoorCameraIp: [
          { required: true, message: "摄像头IP输入不能为空", trigger: "blur" },
          {
            pattern: /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/,
            message: "格式有误请重新输入",
            trigger: "blur",
          },
        ],
        outDoorCameraIp: [
          { required: true, message: "摄像头IP输入不能为空", trigger: "blur" },
          {
            pattern: /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/,
            message: "格式有误请重新输入",
            trigger: "blur",
          },
        ],
        longTimeNoReturn: [
          { required: true, message: "长时间未归还", trigger: "blur" },
          {
            pattern: /^[1-9]\d{0,2}$/,
            message: "格式有误请重新输入",
            trigger: "blur",
          },
        ],
        doorIp: [
          { required: true, message: "感应门IP不能为空", trigger: "blur" },
          {
            pattern: /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/,
            message: "格式有误请重新输入",
            trigger: "blur",
          },
        ],
      },
      myfrom: {
        warehouseName: "",
        inDoorCameraIp: "192.168.0.2",
        outDoorCameraIp: "192.168.0.4",
        longTimeNoReturn: "",
        doorIp: "192.168.0.250",
        warehouseId: "",
      },
      active: 0,
      dialogVisible: true,
      onediv: true,
      twodiv: false,
      buttonshow: false,
      Warehouse: [],
    };
  },
  mounted() {
    this.loading = this.$loading({
      lock: true,
      text: "加载数据中，请稍后",
      spinner: "el-icon-loading",
      background: "rgba(0, 0, 0, 0.7)",
    });
    // 获取本地表单 如果有值就跳转
    if (JSON.parse(localStorage.getItem("myfrom"))) {
      this.$router.push("/Myecharts");
    }
    // 获取所有仓库 数据  首页
    listAllWarehouse().then((responce) => {
      const result = Object.assign(responce);
      if (result.data.length) {
        const res = result.data.map((data) => {
          return {
            warehouseName: data.warehouseName,
            id: data.id,
          };
        });
        this.Warehouse = res;
        // 添加一个值班室
        this.Warehouse.push({ id: "G63", warehouseName: "值班室" });
      } else {
        this.Warehouse = [];
      }
      this.loading.close();
      // 把 所有 仓库信息存到本地  包括值班室
      localStorage.setItem("Warehouse", JSON.stringify(this.Warehouse));
    });
  },
  methods: {
    // 返回按钮
    comeback() {
      this.onediv = true;
      this.twodiv = false;
      this.buttonshow = false;
      this.active = 1;
    },
    // 点击下一步的按钮  三条数据的
    next(item) {
      // 把点击的item 的name和id 复制给表单  包括g63的
      this.myfrom.warehouseName = item.warehouseName;
      this.myfrom.warehouseId = item.id;
      if (item.id === "G63") {
        localStorage.setItem("zbsid", this.myfrom.warehouseId);
        localStorage.setItem("myfrom", JSON.stringify(this.myfrom));
        window.location.reload();
      }
      // 把表单的数据换成当前点击项的

      if (item.warehouseName != "值班室") {
        this.onediv = false;
        this.twodiv = true;
        this.buttonshow = true;
        this.active = 2;
      }
      // 如果点击项为值班室的话， 执行另外的方法
    },
    // 提交表单
    async submitform() {
      this.$refs.myfrom.validate(async (valid) => {
        if (valid) {
          // 提交现在的表单 不包括值班室
          const res = await setConfigByWarehouse(this.myfrom);
          if (res.data === "绑定成功") {
            // 告诉后台现在进去的id
            const res = setShowWarehouseId({
              wareHouseId: this.myfrom.warehouseId,
            });
            console.log(res);
            // 这个是所有仓库的
            localStorage.setItem("setmywarehouseId", this.myfrom.warehouseId);
            localStorage.setItem("myfrom", JSON.stringify(this.myfrom));
            localStorage.setItem("zbsid", this.myfrom.warehouseId);
            window.location.reload();
            this.$message({
              type: "success",
              message: "绑定成功",
            });
          } else {
            this.$message.error(res.data);
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
</style>